package com.hzw.service;

import com.alibaba.fastjson.JSONObject;
import com.hzw.api.IShoppingCarService;
import com.hzw.common.DateUtils;
import com.hzw.dao.ShoppingCarDao;
import com.hzw.entity.ShoppingCar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.swing.text.html.parser.Entity;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ShoppingCarService implements IShoppingCarService {
    @Autowired
    ShoppingCarDao shoppingCarDao;

    @Autowired
    CommodityService commodityService;

    @Override
    public int insert(ShoppingCar shoppingCar) {
        shoppingCar.setId(UUID.randomUUID().toString().replace("-", ""));
        shoppingCar.setCreateTime(DateUtils.getNowTime());
        String json = JSONObject.toJSONString(shoppingCar.getNewCommoduty());
        shoppingCar.setCommodity(json);
        int res = shoppingCarDao.insert(shoppingCar);
        return res;
    }

    @Override
    public int delByUserId(ShoppingCar shoppingCar) {
        int res = shoppingCarDao.delByUserId(shoppingCar);
        return res;
    }

    @Override
    public void addUpdate(ShoppingCar shoppingCar) {
        List<ShoppingCar> shoppingCars = shoppingCarDao.selectByUserId(shoppingCar);
        //如果购物车无商品则添加新购物车
        if (CollectionUtils.isEmpty(shoppingCars)) {
            this.insert(shoppingCar);
        } else {
            Map<String, String> newCommoduty = shoppingCar.getNewCommoduty();
            String commodity = shoppingCars.get(0).getCommodity();
            Map map = JSONObject.parseObject(commodity, Map.class);
            map.putAll(newCommoduty);
            //对库存进行修改
            for (Map.Entry<String, String> m : newCommoduty.entrySet()) {
                int num = Integer.valueOf(m.getValue()).intValue();
                commodityService.changeStock(m.getKey(),num);
            }
            String json = JSONObject.toJSONString(map);
            shoppingCar.setCommodity(json);
            shoppingCarDao.updateByUserId(shoppingCar);
        }
    }

    @Override
    public void update(ShoppingCar shoppingCar) {
        if (!StringUtils.isEmpty(shoppingCar.getNewCommoduty())) {
            String json = JSONObject.toJSONString(shoppingCar.getNewCommoduty());
            shoppingCar.setCommodity(json);
        }
        shoppingCarDao.updateByUserId(shoppingCar);
    }

    @Override
    public List<ShoppingCar> selectByUserId(ShoppingCar shoppingCar) {
        List<ShoppingCar> shoppingCars = shoppingCarDao.selectByUserId(shoppingCar);
        String commodity = null;
        if (CollectionUtils.isEmpty(shoppingCars)) {
            List<ShoppingCar> resList = new ArrayList<>();
            return resList;
        }
        commodity = shoppingCars.get(0).getCommodity();
        Map<String, String> map = JSONObject.parseObject(commodity, Map.class);
        List<Map<String, Object>> resList = new ArrayList<>();
        for (Map.Entry<String, String> m : map.entrySet()) {
            Map<String, Object> commodityMap = new HashMap<>();
            commodityMap.put("commodityName", m.getKey());
            commodityMap.put("num", m.getValue());
            //查询商品详细信息
            String img = commodityService.getImg(m.getKey());
            commodityMap.put("img", img);
            String commodityId = commodityService.getCommodityId(m.getKey());
            commodityMap.put("commodityId", commodityId);
            double price = commodityService.getPrice(m.getKey());
            commodityMap.put("price", price);
            String type = commodityService.getType(m.getKey());
            commodityMap.put("classification", type);
            resList.add(commodityMap);
        }
        shoppingCars.get(0).setCommodityList(resList);
        return shoppingCars;
    }
}
